package com.woorigil.social.domain.deal;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woorigil.social.Context;
import com.woorigil.social.DateUtils;
import com.woorigil.social.domain.deal.repository.DealListCondition;
import com.woorigil.social.domain.deal.repository.DealListDao;

@Service
public class DealFindService {
	@Autowired DealDao dealDao;
	@Autowired DealListDao dealListDao;
	@Autowired RecentQueryDao recentQueryDao;
	@Autowired RecentViewDao recentViewDao;

	public DealList findCategoryDealList(String sid, int mainCategoryId, int subCategoryId, int offset) {
		DealListCondition categoryCondition = new DealListCondition();
		categoryCondition.setCategoryIds(Context.getCategoryIds(mainCategoryId, subCategoryId));
//		int categoryDealTotalCount = dealListDao.selectCategoryTotalCount(categoryCondition);
//		categoryCondition.setTotalItemCount(categoryDealTotalCount);
		categoryCondition.setOffset(offset);
		List<Deal> categoryDeals = dealListDao.selectCategoryList(categoryCondition);
		return new DealList(0, categoryDeals);
	}
	
	public DealList findNewDealList(String sid, int mainCategoryId, int subCategoryId, int offset) {	
		DealListCondition newDealCondition = new DealListCondition();
		newDealCondition.setCategoryIds(Context.getCategoryIds(mainCategoryId, subCategoryId));
		newDealCondition.setSaleStartAtFrom(DateUtils.getYesterday());
		newDealCondition.setSaleStartAtTo(new Date());
//		int newDealTotalCount = dealListDao.selectNewTotalCount(newDealCondition);
//		newDealCondition.setTotalItemCount(newDealTotalCount);
		newDealCondition.setOffset(offset);
		List<Deal> newDeals = dealListDao.selectNewList(newDealCondition);
		return new DealList(0, newDeals);
	}
	
	public DealList findEndDealList(String sid, int mainCategoryId, int subCategoryId, int offset) {	
		DealListCondition endDealCondition = new DealListCondition();
		endDealCondition.setCategoryIds(Context.getCategoryIds(mainCategoryId, subCategoryId));
		endDealCondition.setSaleEndAtFrom(new Date());
		endDealCondition.setSaleEndAtTo(DateUtils.getTomorrow());
		endDealCondition.setOffset(offset);
		List<Deal> newDeals = dealListDao.selectEndList(endDealCondition);
		return new DealList(0, newDeals);
	}
	
	public DealList findFreeDealList(String sid, int mainCategoryId, int subCategoryId, int offset) {		
		DealListCondition freeDealCondition = new DealListCondition();
		freeDealCondition.setCategoryIds(Context.getCategoryIds(mainCategoryId, 0));
		freeDealCondition.setOffset(offset);
		List<Deal> freeDeals = dealListDao.selectFreeList(freeDealCondition);
		int freeDealTotalCount = freeDeals.size();
		
		return new DealList(freeDealTotalCount, freeDeals);
	}

	public List<Deal> findAllFreeDealByCategoryIds(List<Integer> subCategoryIds) {
//		DealCondition condition = new DealCondition();
//		condition.setFree(true);
//		condition.setCategoryIds(subCategoryIds);
//		condition.setItemCountInPage(100);
//		return dealDao.search(condition);
		return null;
	}

	public List<Deal> fidnViewedDealBox(String sid, int offset) {
		List<RecentView> recentViews = recentViewDao.search(sid, offset);
		
		List<Deal> deals = new ArrayList<Deal>();
		for (RecentView recentView : recentViews) {
			Deal deal = dealDao.select(recentView.getDealId());
			if (deal != null) {
				deal.setViewDate(recentView.getInsertDate());
				deals.add(deal);
			}
		}
		return deals;
	}

	public DealList search(String sid, String query, int offset) {
		if (StringUtils.isBlank(query)) {
			return new DealList(0, new ArrayList<Deal>());
		}
		
		RecentQuery recentQuery = recentQueryDao.selectBySidAndQuery(sid, query);
		if (recentQuery == null) {
			recentQueryDao.insert(new RecentQuery(sid, query, new Date()));
		} else {
			recentQuery.setInsertDate(new Date());
			recentQueryDao.updateByQuery(recentQuery);
		}
		
		DealListCondition condition = new DealListCondition();
		condition.setSid(sid);
		condition.setQuery(query);
		condition.setOffset(offset);
		List<Deal> deals = dealListDao.search(condition);
		return new DealList(deals.size(), deals);
	}
}
